Heaps
Tags: #◇Computer Science #◇CCDSALG #◇Heaps #◇Conceptual #◇M5
◇Overview
A binary heap is an array that can be viewed as a nearly complete binary tree. The document details the structure of a max-heap where the parent's key is always greater than or equal to its children's keys. It introduces heap terminology, array indexing methods for traversing the tree, and the heap-size attribute.
◇Important Formulas
1-Based Array Indexing (Classical CLRS):
0-Based Array Indexing (Standard Modern C/C++/Java):
Heap Invariant:
- Max-Heap:
- Min-Heap:
Time & Space Complexities:
- Height of heap with elements:
Max-Heapify/Sift-Down:Build-Max-Heap: (asymptotically tighter than via summation of heights)Insert/Sift-Up:Extract-Max:Heap-Sort: in-place ( auxiliary space)
◇Theoretical Framework: Array & Tree Duality
Operation Lifecycles: Sift-Up vs. Sift-Down
◇Worked Examples
- Array to Tree Mapping: An array
[100, 19, 36, 17, 3, 25, 1, 2, 7]is 1-indexed. The parent of node at index 4 (17) is index (19). The left child of index 2 is index 4 (17), and right child is index 5 (3). - Extract-Max Step: Root
100is removed. Last leaf7(index 9) replaces100at index 1.Max-Heapifycompares7with children19and36. Largest is36(index 3).7swaps with36. Then7compares with children25and1. Largest is25(index 6).7swaps with25. Tree is restored.
◇References & Navigation
- Related Notes: ◇Heaps